package org.dxy.trigger.http;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dxy.api.dto.request.substitute.*;
import org.dxy.api.dto.response.R;
import org.dxy.infrastructure.persistent.po.query.SubstituteMaterialViewPo;
import org.dxy.trigger.http.service.SubstituteMaterialApplicationService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 替代料管理控制器
 * @author dxy
 * @createTime 2025/9/13
 */
@Slf4j
@RestController
@RequestMapping("/api/substituteMaterial")
@RequiredArgsConstructor
public class SubstituteMaterialController {

    private final SubstituteMaterialApplicationService substituteMaterialApplicationService;

    /**
     * 添加替代料
     */
    @PostMapping("/add")
    public R<Void> add(@Validated @RequestBody AddSubstituteMaterialRequest request) {
        return substituteMaterialApplicationService.add(request);
    }

    /**
     * 更新替代料
     */
    @PutMapping("/{id}")
    public R<Void> update(@PathVariable Long id, @Validated @RequestBody UpdateSubstituteMaterialRequest request) {
        return substituteMaterialApplicationService.update(id, request);
    }

    /**
     * 移除替代料
     */
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        return substituteMaterialApplicationService.remove(id);
    }

    /**
     * 审核通过替代料
     */
    @PutMapping("/{id}/approve")
    public R<Void> approve(@PathVariable Long id) {
        return substituteMaterialApplicationService.approve(id);
    }

    /**
     * 审核拒绝替代料
     */
    @PutMapping("/{id}/reject")
    public R<Void> reject(@PathVariable Long id, @Validated @RequestBody RejectSubstituteMaterialRequest request) {
        return substituteMaterialApplicationService.reject(id, request);
    }

    /**
     * 工单应用替代料
     */
    @PutMapping("/{id}/apply-workorder")
    public R<Void> applyToWorkOrder(@PathVariable Long id, @Validated @RequestBody ApplySubstituteMaterialToWorkOrderRequest request) {
        return substituteMaterialApplicationService.applyToWorkOrder(id, request);
    }

    /**
     * 分页查询替代料列表
     */
    @GetMapping("/list")
    public R<Page<SubstituteMaterialViewPo>> list(
            @ParameterObject Page<SubstituteMaterialViewPo> page,
            @ParameterObject SubstituteMaterialViewPo query) {
        return substituteMaterialApplicationService.list(page, query);
    }

    /**
     * 根据ID查询替代料详情
     */
    @GetMapping("/{id}")
    public R<SubstituteMaterialViewPo> getById(@PathVariable Long id) {
        return substituteMaterialApplicationService.getById(id);
    }

    /**
     * 根据MBOM ID查询替代料列表
     */
    @GetMapping("/mbom/{mbomId}")
    public R<List<SubstituteMaterialViewPo>> getByMbomId(@PathVariable Long mbomId) {
        return substituteMaterialApplicationService.getByMbomId(mbomId);
    }

    /**
     * 根据MBOM项ID查询替代料列表
     */
    @GetMapping("/mbom-item/{mbomItemId}")
    public R<List<SubstituteMaterialViewPo>> getByMbomItemId(@PathVariable Long mbomItemId) {
        return substituteMaterialApplicationService.getByMbomItemId(mbomItemId);
    }

    /**
     * 根据状态查询替代料列表
     */
    @GetMapping("/status/{status}")
    public R<List<SubstituteMaterialViewPo>> getByStatus(@PathVariable String status) {
        return substituteMaterialApplicationService.getByStatus(status);
    }

    /**
     * 根据主料编码查询替代料列表
     */
    @GetMapping("/main-material/{mainMaterialCode}")
    public R<List<SubstituteMaterialViewPo>> getByMainMaterialCode(@PathVariable String mainMaterialCode) {
        return substituteMaterialApplicationService.getByMainMaterialCode(mainMaterialCode);
    }

    /**
     * 根据替代料编码查询替代料列表
     */
    @GetMapping("/substitute-material/{substituteMaterialCode}")
    public R<List<SubstituteMaterialViewPo>> getBySubstituteMaterialCode(@PathVariable String substituteMaterialCode) {
        return substituteMaterialApplicationService.getBySubstituteMaterialCode(substituteMaterialCode);
    }

    /**
     * 查询可用的替代料（状态为ACTIVE且在有效期内）
     */
    @GetMapping("/available/{mbomItemId}")
    public R<List<SubstituteMaterialViewPo>> getAvailableSubstitutes(@PathVariable Long mbomItemId) {
        return substituteMaterialApplicationService.getAvailableSubstitutes(mbomItemId);
    }
}